package handler

import (
	"github.com/gin-gonic/gin"
	"lesson/api/request"
	"lesson/internal/dao"
	"lesson/internal/model"
	"lesson/pkg"
	"log"
	"net/http"
)

func List(c *gin.Context) {
	var req request.ListVideo

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "取值失败",
			"data":    err.Error(),
		})
		return
	}
	var data []model.Video

	fields := dao.GetListByFields(&model.Video{
		VideoName:     req.VideoName,
		Learn:         req.Learn,
		Work:          req.Work,
		CType:         req.CType,
		CreatedWorker: req.CreatedWorker,
		AuthorNumber:  req.AuthorNumber,
		Checkout:      req.Checkout,
		Keyword:       req.Keyword,
	}, &data)

	if !fields {
		c.JSON(http.StatusOK, gin.H{
			"code":    1000,
			"message": "视频列表展示失败",
			"data":    nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "视频列表展示成功",
		"data":    data,
	})

}

func AddVideo(c *gin.Context) {

	var req request.AddVideo

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "取值失败",
			"data":    err.Error(),
		})
		return
	}
	data := model.Video{
		VideoName:      req.VideoName,
		VideoUrl:       req.VideoUrl,
		Picture:        req.Picture,
		Learn:          req.Learn,
		Work:           req.Work,
		CType:          0,
		CreatedWorker:  req.CreatedWorker,
		CreateWork:     req.CreateWork,
		AuthorNumber:   req.AuthorNumber,
		Author:         req.Author,
		Checkout:       "",
		Check:          "",
		Keyword:        req.Keyword,
		VideoIntroduce: req.VideoIntroduce,
		Look:           req.Look,
		UsedType:       req.UsedType,
	}

	if !dao.Create(&data) {
		c.JSON(http.StatusOK, gin.H{
			"code":    1000,
			"message": "新增内容失败",
			"data":    nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "新增内容成功",
		"data":    data,
	})

}
func DelVideo(c *gin.Context) {
	var req request.DelVideo
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "取值失败",
			"data":    err.Error(),
		})
		return
	}
	var data model.Video
	if !dao.GetOneById(req.Id, &data) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该视频不存在",
			"data":    nil,
		})
		return
	}

	if !dao.Delete(&data) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该视频删除失败",
			"data":    nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "该视频删除成功",
		"data":    nil,
	})

}

func CheckVideo(c *gin.Context) {
	var req request.CheckVideo

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "取值失败",
			"data":    err.Error(),
		})
		return
	}
	var data model.Video
	if !dao.GetOneById(req.Id, &data) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该视频不存在",
			"data":    nil,
		})
		return
	}

	if !dao.Update(&model.Video{Id: data.Id, CType: req.CType}) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该视频修改失败",
			"data":    nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "该视频修改成功",
		"data":    data,
	})

}

func OneVideo(c *gin.Context) {
	var req request.OneVideo

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "取值失败",
			"data":    err.Error(),
		})
		return
	}
	var data model.Video
	if !dao.GetOneById(req.Id, &data) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "详情展示失败",
			"data":    nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "详情展示成功",
		"data":    data,
	})

}
func UploadVideo(c *gin.Context) {
	file, _ := c.FormFile("file")
	log.Println(file.Filename)

	dst := "./status/img/" + file.Filename
	// 上传文件至指定的完整文件路径
	c.SaveUploadedFile(file, dst)

	if !pkg.UploadImg(dst, file.Filename) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "文件上传失败",
			"data":    nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    0,
		"message": "文件上传成功",
		"data":    file.Filename,
	})

}

func UpdateVideo(c *gin.Context) {
	var req request.UpdateVideo

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "取值失败",
			"data":    err.Error(),
		})
		return
	}
	userId := c.GetUint("userId")
	var user model.User
	if !dao.GetOneById(userId, &user) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该人员不存在",
			"data":    nil,
		})
		return

	}
	data := model.Video{}
	if user.CType == 2 {

		data = model.Video{
			VideoName:      req.VideoName,
			VideoUrl:       req.VideoUrl,
			Picture:        req.Picture,
			Learn:          req.Learn,
			Work:           req.Work,
			CType:          0,
			CreatedWorker:  req.CreatedWorker,
			CreateWork:     req.CreateWork,
			AuthorNumber:   req.AuthorNumber,
			Author:         req.Author,
			Checkout:       "",
			Check:          "",
			Keyword:        req.Keyword,
			VideoIntroduce: req.VideoIntroduce,
			Look:           req.Look,
			UsedType:       req.UsedType,
		}

	} else {
		data = model.Video{
			VideoName:      req.VideoName,
			VideoUrl:       req.VideoUrl,
			Picture:        req.Picture,
			Learn:          req.Learn,
			Work:           req.Work,
			CType:          req.CType,
			CreatedWorker:  req.CreatedWorker,
			CreateWork:     req.CreateWork,
			AuthorNumber:   req.AuthorNumber,
			Author:         req.Author,
			Checkout:       req.Checkout,
			Check:          req.Check,
			Keyword:        req.Keyword,
			VideoIntroduce: req.VideoIntroduce,
			Look:           req.Look,
			UsedType:       req.UsedType,
		}
	}
	if !dao.Update(&model.Video{Id: data.Id}) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该视频修改失败",
			"data":    nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code":    10000,
		"message": "该视频修改成功",
		"data":    nil,
	})
}
